package com.anjubao.parking.municipal.parkinglot.service;


import cn.hutool.core.date.DateUtil;
import com.anjubao.parking.municipal.basezone.dto.DistrictsResDto;
import com.anjubao.parking.municipal.parkinglot.dto.*;
import com.anjubao.parking.municipal.parkinglot.mapper.BaseZoneMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingLotStatusMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingRecordMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingStatSlotMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingStatSlot;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * ParkingStatSlot服务实现类
 * @author anjubao
 */
@Service
public class ParkingStatSlotService extends ServiceImpl<ParkingStatSlotMapper, ParkingStatSlot> {

    @Autowired
    private ParkingStatSlotMapper parkingStatSlotMapper;
    @Autowired
    private BaseZoneMapper baseZoneMapper;
    @Autowired
    private ParkingLotStatusMapper parkingLotStatusMapper;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;

    /**
     * 根据城市id和停车场类型统计当日各类型停车场停车记录总数
     * @param cityId
     * @param type
     * @return
     */
    public List<ParkingStatSlotsFreeResDto> findSlotsFree(Integer cityId, Integer type){
        return parkingLotStatusMapper.findSlotsFree(cityId, type);
    }

    /**
     *  根据城市id统计当日各地区停车场空位
     * @param cityId
     * @return
     */
    public List<ParkingStatSlotsZonesResDto> findSlotsZones(Integer cityId) {
        //查询数据库各地区空位数
        List<ParkingStatSlotsZonesResDto> slotsZones = parkingLotStatusMapper.findSlotsZones(cityId);
        //查询全部区域
        List<DistrictsResDto> districts = baseZoneMapper.findDistricts(cityId);
        //遍历区域list
        for (DistrictsResDto district : districts) {
            ParkingStatSlotsZonesResDto parkingStatSlotsZonesResDto=new ParkingStatSlotsZonesResDto();
            //得到所有区域name
            String districtName=district.getDistrictName();
            Boolean flat=false;
            //遍历数据库中空位数
            for (ParkingStatSlotsZonesResDto slotsZone : slotsZones) {
                //得到区域名称
                String name=slotsZone.getDistrictName();
                if (name.equals(districtName)){
                    flat=true;
                    break;
                }
            }
            if (!flat){
                parkingStatSlotsZonesResDto.setDistrictName(districtName);
                parkingStatSlotsZonesResDto.setFree(0);
                parkingStatSlotsZonesResDto.setTotal(0);
                slotsZones.add(parkingStatSlotsZonesResDto);
            }
        }
        Collections.sort(slotsZones, new Comparator<ParkingStatSlotsZonesResDto>() {
            @Override
            public int compare(ParkingStatSlotsZonesResDto o1, ParkingStatSlotsZonesResDto o2) {
                return o2.getFree()-o1.getFree();
            }
        });
        return slotsZones;
    }

    /**
     * 根据城市id和tab统计重点停车场车位空位列表
     * @param cityId
     * @param tab
     * @return
     */
    public List<ParkingStatSlotsFocusResDto> findSlotsFocus(Integer cityId, Integer tab) {
        return parkingStatSlotMapper.findSlotsFocus(cityId,tab);
    }


    public List<ParkingTrentSlotsUsedResDto> getParkingTrendSlotsUsed(Integer cityId, Integer type) {
        String hour=DateUtil.format(new Date(), "HH");
        if(hour.equals(0)){
            return parkingStatSlotMapper.getParkingTrendSlotsUsed(cityId,type,0);
        }
        return parkingStatSlotMapper.getParkingTrendSlotsUsed(cityId,type,Integer.valueOf(hour)-1);
    }

    public List<ParkingTrentSlotsFreeResDto> getParkingTrendSlotsfree(String parkingLotCode) {
        String hour=DateUtil.format(new Date(), "HH");
        if(hour.equals(0)) {
            return parkingStatSlotMapper.getParkingTrendSlotsfree(parkingLotCode,0);
        }
        return parkingStatSlotMapper.getParkingTrendSlotsfree(parkingLotCode,Integer.valueOf(hour)-1);
    }

    /**
     * 统计包含停车场数和车位数 按地区统计
     @param cityId
      * @return
     */
    public List<ParkingStatDistrictParkingLotResDto> getParkingStatParkingLots(Integer cityId) {
        return parkingLotStatusMapper.getParkingStatParkingLots(cityId);
    }

    /**
     * 统计单位时间泊位周转率
     * @param cityId
     * @param startTime
     * @param endTime
     * @return
     */
    public ParkingLotSlotTurnoverDto getParkingSlotTurnover(Integer cityId, LocalDateTime startTime, LocalDateTime endTime) {
        return parkingRecordMapper.getParkingSlotTurnover(cityId,startTime,endTime);
    }
}
